//package Com.Ring.TradingMobile;
//
//public class HistoryTask {
//
//}
package Com.Ring.TradingMobile;

import java.util.ArrayList;
import java.util.List;

import Com.Ring.TradingMobile.Object.CompanyInfo;
import Com.Ring.TradingMobile.Object.StockInfo;
import Com.Ring.TradingMobile.SQLite.DBManager;
import android.database.Cursor;
import android.util.Log;

public class HistoryTask implements Runnable {
	public HistoryActivity History;
	private boolean IsUpdateCompany;
	private String Code;
	private String TAG = "HistoryDebug";
	private DBManager dbData;// = new DBManager(ThiTruong);
	private String FromDate;
	private String ToDate;

	public HistoryTask(HistoryActivity history,
			String code, String fromDate, String toDate) {
		this.History = history;
		this.Code = code;
		this.FromDate = fromDate;
		this.ToDate = toDate;
		dbData = new DBManager(History);
	}

	public void run() {
		if (this.History.IsUpdateCompany) {
			this.History.ShowDialog();
			Log.d(TAG, "Running Update CompanyList");
			List<CompanyInfo> companyList = GetListCompany();
			try {
				Log.d(TAG, companyList.size() + "");

				this.History.UpdateCompany(companyList);
				this.History.IsUpdateCompany = false;

				Log.d(TAG, "Complete Update CompanyList");

			} catch (Exception ex) {
				Log.d(TAG, ex.toString());
			}
			this.History.RemoveDialog();
		}
		if (!this.Code.equals("") && this.Code.length() >= 3) {
			try {
				Log.d(TAG, "Running Update StockInfo");
				List<StockInfo> stockInfo = GetListStockInfo();
				this.History.UpdateListStockInfo(stockInfo);
			} catch (Exception ex) {
				Log.d(TAG, ex.toString());
			}
		}
		dbData.close();
	}

	private List<CompanyInfo> GetListCompany() {
		List<CompanyInfo> companyList = new ArrayList<CompanyInfo>();
		Cursor cur = dbData.Select("select " + CompanyInfo.CODE + " from "
				+ CompanyInfo.TABLE_NAME);
		if (cur.getCount() > 0) {
			while (cur.moveToNext()) {
				CompanyInfo comp = new CompanyInfo();
				comp.Code = cur.getString(cur.getColumnIndex(CompanyInfo.CODE));
				companyList.add(comp);
			}
		}
		return companyList;
	}
	private List<StockInfo> GetListStockInfo() {
		List<StockInfo> listStockInfo = null;
		Log.d(TAG, "Get Data From Database");
		StringBuilder whereArg = new StringBuilder();
		whereArg.append(StockInfo.SYMBOL);
		whereArg.append("='");
		whereArg.append(Code);
		whereArg.append("'");

		whereArg.append(" and ");
		whereArg.append(StockInfo.TRADE_DATE);
		whereArg.append(">='");
		whereArg.append(this.FromDate);
		whereArg.append("'");

		whereArg.append(" and ");
		whereArg.append(StockInfo.TRADE_DATE);
		whereArg.append("<='");
		whereArg.append(this.ToDate);
		whereArg.append("'");

		String sql = String.format("select * from %s where %s ",
				StockInfo.TABLE_NAME, whereArg);
		Log.d(TAG, sql);
		Cursor listStockCursor = dbData.Select(sql);
		Log.d(TAG,listStockCursor.getCount() +"----");
		if (listStockCursor.getCount() > 0) {
			listStockInfo = new ArrayList<StockInfo>();
			while (listStockCursor.moveToNext()) {
				StockInfo stock = new StockInfo();
				stock.Symbol = listStockCursor.getString(listStockCursor
						.getColumnIndex(StockInfo.SYMBOL));
				stock.RefPrice = listStockCursor.getFloat(listStockCursor
						.getColumnIndex(StockInfo.REF_PRICE));
				stock.CeilPrice = listStockCursor.getFloat(listStockCursor
						.getColumnIndex(StockInfo.CEIL_PRICE));
				stock.FloorPrice = listStockCursor.getFloat(listStockCursor
						.getColumnIndex(StockInfo.FLOOR_PRICE));
				stock.PriceMatch = listStockCursor.getFloat(listStockCursor
						.getColumnIndex(StockInfo.PRICE_MATCH));
				stock.Changed = listStockCursor.getFloat(listStockCursor
						.getColumnIndex(StockInfo.CHANGED));
				stock.PerChanged = listStockCursor.getFloat(listStockCursor
						.getColumnIndex(StockInfo.PER_CHANGED));
				stock.VolumeMatch = listStockCursor.getFloat(listStockCursor
						.getColumnIndex(StockInfo.VOLUME_MATCH));
				stock.TotalMatch = listStockCursor.getFloat(listStockCursor
						.getColumnIndex(StockInfo.TOTAL_MATCH));

				stock.BestPriceBuy = listStockCursor.getFloat(listStockCursor
						.getColumnIndex(StockInfo.BEST_PRICE_BUY));
				stock.BestVolbuy = listStockCursor.getFloat(listStockCursor
						.getColumnIndex(StockInfo.BEST_VOL_BUY));
				stock.BestPriceSell = listStockCursor.getFloat(listStockCursor
						.getColumnIndex(StockInfo.BEST_PRICE_SELL));
				stock.BestVolSell = listStockCursor.getFloat(listStockCursor
						.getColumnIndex(StockInfo.BEST_VOL_SELL));
				stock.TradeDate = listStockCursor.getString(listStockCursor
						.getColumnIndex(StockInfo.TRADE_DATE));
				listStockInfo.add(stock);
			}
		}

		return listStockInfo;

	}

}
